package com.oig.sys.auth.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.auth.dto.ResourceDataCfgDto;
import com.oig.sys.auth.service.ITbResourceDataCfgService;
import com.oig.sys.auth.vo.ResourceDataCfgVo;
import com.oig.sys.auth.vo.UserDataCfgVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "9-菜单数据权限配置")
@RestController
@RequestMapping("/resourceDataCfg")
@Slf4j
public class TbResourceDataCfgController {

    private final ITbResourceDataCfgService resourceDataCfgService;

    public TbResourceDataCfgController(ITbResourceDataCfgService resourceDataCfgService){
        this.resourceDataCfgService = resourceDataCfgService ;
    }


    @Operation(summary = "获取菜单对应的数据权限配置,获取菜单对应的数据权限配置列表")
    @PostMapping(value = "/listByResourceId/{resourceId}")
    public List<ResourceDataCfgVo> listByResourceId(@PathVariable(value = "resourceId") Integer resourceId){
        log.info("query ResourceDataCfg by resourceId={}", resourceId);
        return this.resourceDataCfgService.getResourceDataCfg(resourceId);
    }


    @PreAuthorize("@pms.hasPermission('/resourceDataCfg/addResourceDataCfg')")
    @SysLog(value="增加菜单数据权限配置",apiUrl="/resourceDataCfg/addResourceDataCfg")
    @Operation(summary = "增加菜单数据权限配置")
    @PostMapping(value = "/addResourceDataCfg")
    public ResourceDataCfgVo addResourceDataCfg(@RequestBody @Validated({GroupAdd.class}) ResourceDataCfgDto resourceDataCfgDto){
        log.info("add ResourceDataCfg {}", resourceDataCfgDto);
        return this.resourceDataCfgService.addResourceDataCfg(resourceDataCfgDto);
    }

    @PreAuthorize("@pms.hasPermission('/resourceDataCfg/updateResourceDataCfg')")
    @SysLog(value="修改数据权限配置",apiUrl="/resourceDataCfg/updateResourceDataCfg")
    @Operation(summary = "修改数据权限配置")
    @PostMapping(value = "/updateResourceDataCfg")
    public ResourceDataCfgVo updateResourceDataCfg(@RequestBody @Validated({GroupEdit.class}) ResourceDataCfgDto resourceDataCfgDto){
        log.info("update ResourceDataCfg {}", resourceDataCfgDto);
        return this.resourceDataCfgService.updateResourceDataCfg(resourceDataCfgDto);
    }

    @PreAuthorize("@pms.hasPermission('/resourceDataCfg/cancelResourceDataCfg')")
    @SysLog(value="逻辑删除数据权限配置",apiUrl="/resourceDataCfg/cancelResourceDataCfg")
    @Operation(summary = "逻辑删除数据权限配置")
    @PostMapping(value = "/cancelResourceDataCfg/{cfgId}")
    public SysResponse<Integer> cancelResourceDataCfg(@PathVariable("cfgId") Integer cfgId){
        log.info("cancel ResourceDataCfg cfgId:{}",cfgId);
        this.resourceDataCfgService.cancelResourceDataCfg(cfgId);
        return SysResponse.ok(cfgId,"删除成功");
    }

    @PreAuthorize("@pms.hasPermission('/resourceDataCfg/revertResourceDataCfg')")
    @SysLog(value="恢复数据权限配置",apiUrl="/resourceDataCfg/revertResourceDataCfg")
    @Operation(summary = "恢复数据权限配置")
    @PostMapping(value = "/revertResourceDataCfg/{cfgId}")
    public SysResponse<Integer> revertResourceDataCfg(@PathVariable("cfgId") Integer cfgId){
        log.info("revert ResourceDataCfg cfgId:{}",cfgId);
        this.resourceDataCfgService.revertResourceDataCfg(cfgId);
        return SysResponse.ok(cfgId,"恢复成功");
    }

    @PreAuthorize("@pms.hasPermission('/resourceDataCfg/delResourceDataCfg')")
    @SysLog(value="物理删除数据权限配置",apiUrl="/resourceDataCfg/delResourceDataCfg")
    @Operation(summary = "物理删除数据权限配置")
    @PostMapping(value = "/delResourceDataCfg/{cfgId}")
    public SysResponse<Integer> delResourceDataCfg(@PathVariable("cfgId") Integer cfgId){
        log.warn("del ResourceDataCfg id={}", cfgId) ;
        this.resourceDataCfgService.delResourceDataCfg(cfgId);
        return SysResponse.ok(cfgId,"物理删除成功");
    }


    @Hidden
    @Operation(summary = "获取菜单的权限配置,获取菜单的权限配置")
    @PostMapping(value = "/getUserDataCfg/{resourceId}")
    public UserDataCfgVo getUserDataCfg(@PathVariable(value = "resourceId") Integer resourceId){
        log.info("getUserDataCfg resourceId={}", resourceId);
        return this.resourceDataCfgService.getUserDataCfg(resourceId);
    }


}
